﻿
function ValidateForm() {
    var allValid = true;

    $('div[id$=_Error]').hide();
    $('div[id$=_Match]').hide();
    $('div[id$=_MinMax]').hide();
    $('div[id$=_Range]').hide();

    $("input[validation]").each(function (index) {
        var valid = Validate(this);
        if (!valid) {
            allValid = false;
        }
    });
    

    $("select[validation]").each(function (index) {
        var valid = Validate(this);
        if (!valid) {
            allValid = false;
        }
    });


    $("textarea[validation]").each(function (index) {
        var valid = Validate(this);
        if (!valid) {
            allValid = false;
        }
    });

    return allValid;
}

function Validate(obj) {
    var vTypes = $(obj).attr("validation").split(',');
    var failCount = 0;

    $(vTypes).each(function (rule) {
        var ruleType = vTypes[rule];

        if (ruleType == "required") {

            var isVal = true;

            if ($(obj).val() == null) {
                var nameError = "#" + $(obj).attr("id") + "_Error";
                $(nameError).show();
                failCount += 1;
                return false;
            }

            if ($(obj).val() == "Please select") {
                isVal = false;
            }

            if ($(obj).val().length < 1) {
                isVal = false;
            }

            if (isVal == false) {

                var nameError = "#" + $(obj).attr("id") + "_Error";
                $(nameError).show();
                failCount += 1;
            }

            return isVal;
        }

        if (ruleType == "match") {
            var obj2Id = "#" + $(obj).attr("id") + "2";

            if ($(obj).val() == $(obj2Id).val()) {
                return true;
            }
            var nameError = "#" + $(obj).attr("id") + "_Match";
            $(nameError).show();
            failCount += 1;
            return false;
        }

        if (ruleType == "minmaxLenght") {

            var min = vTypes[rule + 1];
            var max = vTypes[rule + 2];
            var objLenght = $(obj).val().length;
            if (objLenght >= min && objLenght <= max) {
                return true;
            }
            var nameError = "#" + $(obj).attr("id") + "_Error";
            $(nameError).show();
            failCount += 1;
            return false;
        }

        if (ruleType == "range") {

            var min = parseInt(vTypes[rule + 1]);
            var max = parseInt(vTypes[rule + 2]);

            if ($(obj).val() == null) {
                var nameError = "#" + $(obj).attr("id") + "_Error";
                $(nameError).show();
                failCount += 1;
                return false;
            }

            var valObj = parseInt($(obj).val());

            if (valObj >= min && valObj <= max) {
                return true;
            }
            var nameError = "#" + $(obj).attr("id") + "_Error";
            $(nameError).show();
            failCount += 1;
            return false;
        }

        if (ruleType == "email") {
            var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

            if (emailReg.test($(obj).val())) {
                return true;
            }
            var nameError = "#" + $(obj).attr("id") + "_Error";
            $(nameError).show();
            failCount += 1;
            return false;
        }

        if (ruleType == "phone") {
            phoneRegex = /^[+]?[0-9]+[\-]?[0-9]+$/; //US phone regex   /^\(?(\d{3})\)?[- ]?(\d{4})[- ]?(\d{6})$/;

            if (phoneRegex.test($(obj).val())) {
                return true;
            }
            var nameError = "#" + $(obj).attr("id") + "_Error";
            $(nameError).show();
            failCount += 1;
            return false;
        }

        return true;
    });

    if (failCount > 0) {
        return false;
    }
    return true;
}